package com.etl.dataflow.scheduler.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etl.dataflow.common.response.R;
import com.etl.dataflow.scheduler.admin.entity.DataflowTaskInfo;
import com.etl.dataflow.scheduler.admin.entity.request.PageRequest;
import com.etl.dataflow.scheduler.admin.entity.request.SpecificTasksRequest;
import com.etl.dataflow.scheduler.admin.service.DataflowTaskInfoService;
import com.etl.dataflow.scheduler.admin.validation.Add;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.text.ParseException;

/**
 * <p>
 * 任务信息 前端控制器
 * </p>
 *
 * @author dx
 * @since 2022-04-24
 */
@RestController
@RequestMapping("/admin/task-info")
public class DataflowTaskInfoController implements DefaultController {
    private final DataflowTaskInfoService taskInfoService;

    public DataflowTaskInfoController(DataflowTaskInfoService taskInfoService) {
        this.taskInfoService = taskInfoService;
    }

    @PostMapping("/addOrUpdate")
    public R add(@RequestBody @Validated(Add.class) DataflowTaskInfo taskInfo) {
        return taskInfoService.addOrUpdate(taskInfo);
    }

    @GetMapping("/remove")
    public R remove(@RequestParam @NotNull Long id) {
        return successOrError(taskInfoService.removeById(id));
    }

    @GetMapping("/find")
    public R find(@RequestParam @NotEmpty String id) {
        DataflowTaskInfo taskInfo = taskInfoService.getById(id);
        return R.success().put("data", taskInfo);
    }

    @PostMapping("/paging")
    public R paging(@RequestBody @Validated PageRequest pageRequest) {
        Page<? extends DataflowTaskInfo> page = taskInfoService.page(pageRequest);
        return R.success().put("data", page);
    }

    @GetMapping("/run")
    public R runTask(@RequestParam @NotNull Long taskId) {
        return taskInfoService.runTask(taskId);
    }

    @PostMapping("/runTasks")
    public R runSpecificTasks(@RequestBody @Validated SpecificTasksRequest request) {
        return taskInfoService.runSpecificTasks(request.getTaskIds());
    }

    @GetMapping("/runAll")
    public R runAllTask() {
        return taskInfoService.runAllTask();
    }

    @GetMapping("/enableTimer")
    public R enableTimer(@RequestParam @NotNull Long taskId) throws ParseException {
        return taskInfoService.enableTimer(taskId);
    }

    @GetMapping("/disableTimer")
    public R disableTimer(@RequestParam @NotNull Long taskId) {
        return taskInfoService.disableTimer(taskId);
    }

    @PostMapping("/enableSelectedTimers")
    public R enableSpecificTimers(@RequestBody @Validated SpecificTasksRequest request) throws ParseException {
        return taskInfoService.enableSpecificTimers(request.getTaskIds());
    }

    @PostMapping("disableSpecificTimers")
    public R disableSpecificTimers(@RequestBody @Validated SpecificTasksRequest request) {
        return taskInfoService.disableSpecificTimers(request.getTaskIds());
    }

    @GetMapping("/disableAllTimer")
    public R disableAllTimer() {
        return taskInfoService.disableAllTimer();
    }

    @GetMapping("/children")
    public R getChildren(@RequestParam @NotNull Long groupId) {
        return taskInfoService.getChildren(groupId);
    }

    @GetMapping("/terminate")
    public R terminateTask(@RequestParam @NotNull Long taskRecordId, @RequestParam @NotBlank String executorUuid, @RequestParam @NotNull Long taskId) {
        return taskInfoService.terminateTask(taskRecordId, executorUuid, taskId);
    }

}
